package qianfeng.com.meijiadaka;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.lemon.view.RefreshRecyclerView;
import cn.lemon.view.adapter.Action;
import qianfeng.com.meijiadaka.adapter.OneDayAdapter;
import qianfeng.com.meijiadaka.adapter.Tutorial_activityAdapter;
import qianfeng.com.meijiadaka.bean.OneDayBean;
import qianfeng.com.meijiadaka.interfacebean.OneDayData;
import retrofit.GsonConverterFactory;
import retrofit.Retrofit;
import retrofit.RxJavaCallAdapterFactory;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public class Tutorial_activityActivity extends AppCompatActivity {

    @BindView(R.id.activity_title)
    TextView mActivityTitle;
    @BindView(R.id.avtivity_ib)
    ImageButton mAvtivityIb;
    @BindView(R.id.activity_rrv)
    RefreshRecyclerView mRecyclerView;
    private Tutorial_activityAdapter mAdapter;
    private String mUri="http://home.naildaka.com/vapi/v4/home/tutorials/category/videos?pageNo=1&categoryId=";
    private int mId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tutorial_activity);
        ButterKnife.bind(this);
        mId = getIntent().getIntExtra("id", 1);
        String title = getIntent().getStringExtra("title");
        mActivityTitle.setText(title);
        init();
    }

    private void init() {
        mAdapter = new Tutorial_activityAdapter(this);
        mRecyclerView.setSwipeRefreshColors(0xFF437845, 0xFFE44F98, 0xFF2FAC21);
        mRecyclerView.setLayoutManager(new GridLayoutManager(this,2,LinearLayoutManager.VERTICAL,false));
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setRefreshAction(new Action() {
            @Override
            public void onAction() {
                loadData();
            }
        });
        mRecyclerView.post(new Runnable() {
            @Override
            public void run() {
                mRecyclerView.showSwipeRefresh();
                loadData();
            }
        });
    }

    private void loadData() {
        new Retrofit.Builder().baseUrl("http://home.naildaka.com/").addCallAdapterFactory(RxJavaCallAdapterFactory.create()).
                addConverterFactory(GsonConverterFactory.create()).build().create(OneDayData.class).getData(mUri+mId).
                subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<OneDayBean>() {
            @Override
            public void call(OneDayBean oneDayBean) {
                List<OneDayBean.ResultBean> result = oneDayBean.getResult();
                Log.e("afaf","afafa");
                mAdapter.clear();
                mAdapter.addAll(result);
                mRecyclerView.dismissSwipeRefresh();
            }
        }, new Action1<Throwable>() {
            @Override
            public void call(Throwable throwable) {
                Log.e("afa","error");
            }
        });
    }

    @OnClick(R.id.avtivity_ib)
    public void onClick() {
        finish();
    }
}
